package Exercise;

import java.util.concurrent.locks.LockSupport;

public class Demo03_park的预处理_02 {
    public static void main(String[] args) throws Exception {
        Thread t1 = new Thread("t1") {
            @Override
            public void run() {
                try {
                    // ②睡眠2s,线程执行权回到main线程
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                // ⑤暂停当前线程(无效,因为之前预处理过一次unpark)
                System.out.println("park-01-before: " + System.currentTimeMillis());
                LockSupport.park();
                System.out.println("park-01-after: " + System.currentTimeMillis());

                // ⑥这一次的park操作并不会被unpark唤醒,因为unpark的预处理只能进行一次
                System.out.println("park-02-before: " + System.currentTimeMillis());
                LockSupport.park();
                System.out.println("park-02-after: " + System.currentTimeMillis());
            }
        };

        t1.start();

        // ①确保t1线程先执行
        Thread.sleep(10);

        // ③解除t1线程的暂停(相当于预处理一次unpark)
        LockSupport.unpark(t1);

        // ④只允许进行一次unpark的预处理,这一次unpark操作无效
        LockSupport.unpark(t1);
    }
}
